package com.yswl.pri.picture.net;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.yswl.pri.picture.glide.ImgUtils;

import java.io.File;
import java.util.concurrent.ExecutionException;

import yswl.com.klibrary.util.L;
import yswl.com.klibrary.util.ToastUtil;

public class PictrueSaveAsyTask extends AsyncTask<String, Void, String> {
    private static final String TAG = PictrueSaveAsyTask.class.getSimpleName();
    Context context;
    public PictrueSaveAsyTask( Context context) {
        this.context = context;
    }

    @Override
        protected String doInBackground(String... voids) {
            L.e(TAG, " run threadId = " + Thread.currentThread().getId());
            File file = null;
            try {
                file = Glide.with(context)
                        .download(voids[0])
                        .submit()
                        .get();
                return ImgUtils.saveImageToGallery(context, file).getPath();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return "";
        }

        @Override
        protected void onPostExecute(String path) {
            if (!TextUtils.isEmpty(path)) ToastUtil.showToast("图片保存到 " + path);
        }
    }
